define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'wddj/certified_driving_agentt/index' + location.search,
                    add_url: 'wddj/certified_driving_agentt/add',
                    edit_url: 'wddj/certified_driving_agentt/edit',
                    multi_url: 'wddj/certified_driving_agentt/multi',
                    import_url: 'wddj/certified_driving_agentt/import',
                    examine_url: 'wddj/certified_driving_agentt/examine',
                    details_url: 'wddj/certified_driving_agentt/details',
                    table: 'wddj_certified_driving_agentt_trailer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'id_card', title: __('Id_card'), operate: false},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'driving_experience', title: __('Driving_experience'), searchList: {"1年":__('Driving_experience 1年'),"2年":__('Driving_experience 2年'),"3年":__('Driving_experience 3年'),"4年":__('Driving_experience 4年'),"5年":__('Driving_experience 5年'),"5年以上":__('Driving_experience 5年以上')}, formatter: Table.api.formatter.normal},
                        {field: 'driver_license_type', title: __('Driver_license_type'), searchList: {"A1":__('Driver_license_type A1'),"A2":__('Driver_license_type A2'),"A3":__('Driver_license_type A3'),"B1":__('Driver_license_type B1'),"B2":__('Driver_license_type B2'),"C1":__('Driver_license_type C1'),"C2":__('Driver_license_type C2')}, formatter: Table.api.formatter.normal},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'state', title: __('State'), searchList: {"1":__('State 1'),"2":__('State 2'),"3":__('State 3')}, formatter: Table.api.formatter.normal},
                        {field: 'reject', title: __('Reject'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.normal},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'examine',
                                    text: __('审核'),
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa',
                                    extend: 'data-area=["100%","100%"]',
                                    url: 'wddj/certified_driving_agentt/examine',
                                    visible: function (row) {
                                        if(row.state == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        $(".btn-refresh").trigger("click");
                                        return true;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'details',
                                    text: __('查看详情'),
                                    title: __('查看详情'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa',
                                    extend: 'data-area=["100%","100%"]',
                                    url: 'wddj/certified_driving_agentt/details',
                                    visible: function (row) {
                                        if(row.child_state != '1' && row.child_state != '4'){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $(document).on('click','#select-user',function () {
                Fast.api.open('wddj/certified_driving_agentt/select_wechat','选择用户',{
                    area:['80%','95%'],
                    callback:function(data){
                        console.log(data);
                        if(data){
                            $('#c-uid').val(data.id);
                            $('#c-user').val(data.nickname);
                        }else{
                            Layer.alert("请选择用户");
                        }
                    }
                });
            });
        },
        edit: function () {
            Controller.api.bindevent();
        },
        examine: function () {
            Controller.api.bindevent();
        },
        details: function () {
            Controller.api.bindevent();
        },
        select_wechat:function(){
            Table.api.init({
                extend: {
                }
            });
            var table = $("#table");
            table.bootstrapTable({
                url: 'wddj/certified_driving_agentt/select_wechat' + location.search,
                pk: 'id',
                sortName: 'createtime',
                search:false,
                showToggle: false,
                showColumns: false,
                visible: false,
                showExport: false,
                searchFormVisible: true,
                sortOrder:'desc',
                columns: [
                    [
                        {field: 'id', title: __('ID'), sortable: true,operate: false},
                        {field: 'avatar', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'nickname', title: __('昵称'), operate: 'LIKE'},
                        {field: 'mobile', title: __('手机号'), operate: 'LIKE'},
                        {field: 'createtime', title: __('创建时间'),operate: false, formatter: Table.api.formatter.datetime, addclass: 'datetimerange', sortable: true},
                        {
                            field: 'buttons',
                            width: "80px",
                            title: __('操作'),
                            text: __('操作'),
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('选择'),
                                    title: __('选择'),
                                    classname: 'btn btn-xs btn-primary btn-magic btn-click',
                                    icon: 'fa fa-check',
                                    click:function (obj,row) {
                                        console.log(row);
                                        Fast.api.close(row);
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        }
                    ]
                ]
            });
            Table.api.bindevent(table);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $(document).on('change','input[name="row[state]"]',function (){
                    var type=$(this).val();
                    switch (type){
                        case "1":
                        case "2":
                            $('.reject').addClass('hide');
                            break;
                        case "3":
                            $('.reject').removeClass('hide');
                            break;

                    }
                });
                $('body').on('click', '[data-tips-image]', function () {
                    var img = new Image();
                    var imgWidth = this.getAttribute('data-width') || '480px';
                    img.onload = function () {
                        var $content = $(img).appendTo('body').css({background: '#fff', width: imgWidth, height: 'auto'});
                        Layer.open({
                            type: 1, area: imgWidth, title: false, closeBtn: 1,
                            skin: 'layui-layer-nobg', shadeClose: true, content: $content,
                            end: function () {
                                $(img).remove();
                            },
                            success: function () {

                            }
                        });
                    };
                    img.onerror = function (e) {

                    };
                    img.src = this.getAttribute('data-tips-image') || this.src;
                });
            }
        }
    };
    return Controller;
});
